package dao;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import beans.HistoryJob;
import beans.HistoryTask;
import beans.HttpResultList;
import beans.Task;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 *
 */
public class HistoryTaskDao implements Serializable {
	private static final long serialVersionUID = 1204068804746133347L;

	public List<Object> getHistory_Tasks(Integer onePageCount,Integer currentPage){
		String sql = "select task_hash,task_id,model.model_id,model_name,state,history_task.name,user.user_name,start_time,end_time,history_task.is_nesting,history_task.introduction,statement,algorithm_order from `history_task`, user, model where `history_task`.model_id=model.model_id and `history_task`.create_person = user.user_id and  " +
				"batch_task_hash is null order by start_time desc limit "+(currentPage - 1) * onePageCount + " , " +onePageCount;
		List<Object> history_tasks = new ArrayList<>();
		history_tasks = Query.executeQueryList(history_tasks, sql, null, new QueryCallBackList(){
			@Override
			public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
				try {
					while (rs.next()) {
						HistoryTask ht = new HistoryTask(rs.getString("task_hash"), rs.getInt("task_id"),
								rs.getString("model_name"), rs.getString("state"),rs.getString("name"),
								rs.getString("user_name"), rs.getString("start_time"),rs.getString("end_time"),
								null, rs.getBoolean("is_nesting"), null, rs.getString("introduction"),
								rs.getString("statement"),rs.getString("algorithm_order"));
						results.add(ht);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return results;
			}
		});
		return history_tasks;
	}

	public int historyTaskPageNum(Integer onePageCount){
		String sql = "select count(*) from `history_task` where batch_task_hash is null";
		int historyTaskNum = PublicDao.oneNum(sql,null);
		return (historyTaskNum % onePageCount)==0? (historyTaskNum / onePageCount) : (historyTaskNum / onePageCount + 1);
	}

	/**
	 * 统计近一周任务
	 * @author 黄振 2019/7/3
	 * @return weeInfo
	 */
	public JSONObject getHistory_Tasks_Week(){
		String sql = "SELECT unix_timestamp(start_time) as start_time, statement,count(1) as count from history_task " +
				"where DATE_SUB(CURDATE(), INTERVAL 6 DAY) <= date(start_time) and not ISNULL(statement)" +
				"GROUP BY TO_DAYS(start_time),statement";
		return (JSONObject)Query.executeQueryObject(sql, null, new QueryCallBack() {
			@Override
			public Object doExecute(ResultSet rs) {
				Date date = new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
				JSONObject weekInfo = new JSONObject();
				for(int i=0; i<7; i++){
					JSONObject temp = new JSONObject();
					temp.put("finished", 0);
					temp.put("stopped", 0);
					temp.put("pause", 0);
					temp.put("running", 0);
					weekInfo.put(i, temp);
				}
				try {
					while (rs.next()) {
						long startTime = rs.getLong("start_time");
						String statement = rs.getString("statement");
						int count = rs.getInt("count");
						long endDay = sdf.parse(sdf.format(date)).getTime();
						long startDay = sdf.parse(sdf.format(new Date(startTime*1000))).getTime();
						String key = String.valueOf(6 - (endDay-startDay)/(24*60*60*1000));
						((JSONObject)weekInfo.get(key)).put(statement, count);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return weekInfo;
			}
		});
	}

	/**
	 * 统计近一天任务
	 * @author 黄振 2019/7/3
	 * @return weeInfo
	 */
	public JSONObject getHistory_Tasks_Day(){
		String sql = "SELECT HOUR(NOW())-HOUR(start_time) as start_time, statement, count(1) as count from history_task " +
				"where DATE_SUB(NOW(), INTERVAL (11*60+MINUTE(NOW()))*60+SECOND(NOW()) SECOND) <= start_time " +
				"and not ISNULL(statement) GROUP BY HOUR(start_time),statement";
		return (JSONObject)Query.executeQueryObject(sql, null, new QueryCallBack() {
			@Override
			public Object doExecute(ResultSet rs) {
				JSONObject dayInfo = new JSONObject();
				for(int i=0; i<12; i++){
					JSONObject temp = new JSONObject();
					temp.put("finished", 0);
					temp.put("stopped", 0);
					temp.put("pause", 0);
					temp.put("running", 0);
					dayInfo.put(i, temp);
				}
				try {
					while (rs.next()) {
						int startTime = rs.getInt("start_time");
						String statement = rs.getString("statement");
						int count = rs.getInt("count");
						if(startTime<0){
							startTime += 24;
						}
						((JSONObject)dayInfo.get(String.valueOf(startTime))).put(statement, count);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return dayInfo;
			}
		});
	}

	/**
	 * 统计近一天任务
	 * @author 黄振 2019/7/3
	 * @return weeInfo
	 */
	public JSONObject getHistory_Tasks_Hour(){
		String sql = "SELECT unix_timestamp(start_time) as start_time, statement from history_task " +
				"where ? <= start_time and not ISNULL(statement)";
		Date date = new Date(new Date().getTime() - 60*60*1000);
		return (JSONObject)Query.executeQueryObject(sql, new Object[]{date}, new QueryCallBack() {
			@Override
			public Object doExecute(ResultSet rs) {
				Date date = new Date();
				JSONObject hourInfo = new JSONObject();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
				for(int i=0; i<6; i++){
					JSONObject temp = new JSONObject();
					temp.put("finished", 0);
					temp.put("stopped", 0);
					temp.put("pause", 0);
					temp.put("running", 0);
					hourInfo.put(i, temp);
				}
				try {
					while (rs.next()) {
						long startTime = rs.getLong("start_time");
						long startDay = sdf.parse(sdf.format(new Date(startTime*1000))).getTime();
						long endDay = sdf.parse(sdf.format(date)).getTime();
						String key = String.valueOf(5 - (endDay-startDay)/(10*60*1000));
						String statement = rs.getString("statement");
						JSONObject jsonObject = ((JSONObject)hourInfo.get(key));
						if(jsonObject.containsKey(statement)){
							int count = (int)jsonObject.get(statement) + 1;
							jsonObject.put(statement, count);
						}else {
							((JSONObject)hourInfo.get(key)).put(statement, 1);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return hourInfo;
			}
		});
	}


	/**
	 * by zhuyaokun 其中statement值为 finished、stopped、pause、running
	 * @param task
	 * @param taskHash
	 * @return
	 */
	public boolean addHistoryByTask(Task task, String taskHash, String orders, String batchTaskHash) {
		String sql = "insert into `history_task` (statement,task_hash,task_id,model_id,name,"
				+ "create_person,start_time,is_nesting,introduction,batch_task_hash,json) "
				+ "values (?,?,?,?,?,?,NOW(),?,?,?,?)";
		Object[] params = new Object[]{"running", taskHash,task.getTask_id(),task.getModel_id(),
				task.getName(),task.getUserId(),task.isIs_nesting(),
				task.getIntroduction(),batchTaskHash,task.getJson()};
		int result = Query.executeDML(sql, params);
		return result > 0;
	}

	/**
	 * 批量插入子任务
	 */
	public boolean addSubTaskByBatch(Task task, List<String> subTaskHashList, String batchTaskHash) {
		String sql = "insert into `history_task` (task_hash,task_id,model_id,name,create_person,start_time," +
				"is_nesting,introduction,statement,batch_task_hash) values (?,?,?,?,?,NOW(),?,?,?,?)";

		ArrayList<Object[]> params = new ArrayList<>();
		for(String taskHash:subTaskHashList){
			Object o[] = new Object[]{taskHash, task.getTask_id(),task.getModel_id(),task.getName(),task.getUserId(),
					task.isIs_nesting(),task.getIntroduction(),"running",batchTaskHash};
			params.add(o);
		}
		int result = Query.executeBatchDML(sql, params);
		return result > 0;
	}

	public boolean addHistoryByTask(Task task, List<String> taskHash, String orders, String batchTaskHash) {
		StringBuilder sb=new StringBuilder("insert into `history_task` (statement,task_hash,task_id,model_id,name,");
		sb.append("create_person,start_time,is_nesting,introduction,algorithm_order,batch_task_hash) ");
		sb.append("values");
		/*String sql = "insert into `history_task` (statement,task_hash,task_id,model_id,name,"
				+ "create_person,start_time,json,is_nesting,nest_json,introduction,algorithm_order,batch_task_hash) "
				+ "values ";*/
		for(String s: taskHash){
			//sb.append("()
			sb.append("(\"running\"");
			sb.append("\""+s+"\",");
			sb.append(task.getTask_id()+",");
			sb.append(task.getModel_id()+",");
			sb.append("\""+task.getName()+"\",");
			sb.append("\""+task.getUserId()+"\",");
			sb.append("?,");
			sb.append(task.isIs_nesting()+",");
			sb.append("?,");
			sb.append("\""+task.getIntroduction()+"\",");
			sb.append("?,");
			sb.append("\""+batchTaskHash+"\"),");
		}
		Object[] params = new Object[3*taskHash.size()];
		int size=3*taskHash.size();
		for(int i=0;i<size;i++){
			if(i%3==0){
				params[i]= task.getJson();
			}else if(i%3==1){
				params[i]=task.getNest_json();
			}else if(i%3==2){
				params[i]=orders;
			}
		}
//		{"running", taskHash,task.getTask_id(),task.getModel_id(),
//				task.getName(),task.getUserId(),task.getJson(),task.isIs_nesting(),
//				task.getNest_json(),task.getIntroduction(),orders,batchTaskHash};
		int result = Query.executeDML(sb.substring(0, sb.length()-1), params);
		return (result>0)?true:false;
	}
	/**
	 * by zhuyaokun
	 * @param state
	 * @param taskHash
	 * @return
	 */
	public boolean updateRunningHistoryTaskState(String state,String taskHash){
		String sql = "update `history_task` set state = ? where task_hash = ?";
		return Query.executeDML(sql, new Object[]{state,taskHash})>0;
	}
	/**
	 * 更新历史运行任务数据表里面的数据，finished、stopped、pause、running
	 * 同时保证子任务都完成的时候，批量任务的状态也更新
	 * @param statement
	 * @param taskHash
	 * @return
	 */
	public boolean updateHistoryTaskStatement(String statement,String taskHash,String batchHash){
		String sql = "";
		if(statement.equals("finished")||statement.equals("stopped"))
		{
			sql = "update `history_task` set statement = ?, end_time = now()  where task_hash = ?";
		}else{
			sql = "update `history_task` set statement = ? where task_hash = ?";
		}
		int result = Query.executeDML(sql, new Object[]{statement, taskHash});
		if(batchHash!=null){
			//这一句的意思是：查询history_task表里面batch_task_hash字段为batchHash的所有行中，是不是end_time为NULL的只有0条，
			// 是的话，把task_hash值为batchHash的行的end_time设为当前时间
			//不是的话，设为null
			//大白话就是批量任务的所有子任务是不是都完成了，完成的话就将批量任务的结束时间加一下
			String  updateSql = "UPDATE history_task SET end_time = ( CASE WHEN ( SELECT a.aa FROM ( SELECT count(*) aa FROM" +
					" `history_task` WHERE batch_task_hash = ? AND end_time IS NULL ) a ) = 0 " +
					"THEN now() ELSE NULL END ) WHERE task_hash = ?";
			Object[] params = new Object[]{batchHash,batchHash};
			Query.executeDML(updateSql,params);
		}
		return result>0;
	}

	/**
	 * 更新历史运行任务数据表里面的数据，finished、stopped、pause、running
	 * 同时保证子任务都完成的时候，批量任务的状态也更新
	 * @param state
	 * @param taskHash
	 * @return
	 */
	public boolean updateHistoryTaskState(String state,String taskHash,String batchHash, String statement){
		String sql = "";
		if(statement.equals("finished")||statement.equals("stopped"))
		{
			//终止时，finished状态不变。
			sql = "update `history_task` set state = ?, end_time = now(), statement = case when statement='finished' then 'finished' else ? end where task_hash = ?";
		}else{
			//暂停时，finished状态不变。
			sql = "update `history_task` set state = ?,statement = case when statement='finished' then 'finished' else ? end where task_hash = ?";
		}
		int result = Query.executeDML(sql, new Object[]{state, statement, taskHash});
		if(batchHash!=null){
			//这一句的意思是：查询history_task表里面batch_task_hash字段为batchHash的所有行中，是不是end_time为NULL的只有0条，
			// 是的话，把task_hash值为batchHash的行的end_time设为当前时间
			//不是的话，设为null
			//大白话就是批量任务的所有子任务是不是都完成了，完成的话就将批量任务的结束时间加一下
			/**String  updateSql = "UPDATE history_task SET end_time = ( CASE WHEN ( SELECT a.aa FROM ( SELECT count(*) aa FROM" +
			 " `history_task` WHERE batch_task_hash = ? AND end_time IS NULL ) a ) = 0 " +
			 "THEN now() ELSE NULL END ) WHERE task_hash = ?";**/
			//2018年12-18修改，由于上面较为复杂的sql语句在高并发的环境下会导致数据库索引加锁的死锁，详细见：https://blog.csdn.net/zheng0518/article/details/54695605
			//于是将上面的sql语句拆分，变成下面的语句
			String querySql = "select count(*) from `history_task` where batch_task_hash = ? and end_time IS NULL";
			Object[] queryParams = new Object[]{batchHash};
			int queryResult = (int) Query.executeQueryObject(querySql, queryParams, new QueryCallBack() {
				@Override
				public Object doExecute(ResultSet rs) {
					int result = -1;
					try {
						while(rs.next()) {
							result = rs.getInt(1);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return result;
				}
			});
			if(queryResult==0)
			{
				String updateSql = "UPDATE history_task SET end_time = NOW() WHERE task_hash = ?";
				Object[] params = new Object[]{batchHash};
				Query.executeDML(updateSql,params);
			}
		}
		return result>0;
	}
	public JSONArray getHistoryBatchTaskState(String batchTaskHash){
		HttpResultList subTaskHashes = getSubTaskHashByBatchTaskHash(batchTaskHash);
		JSONArray resultArray = new JSONArray();
		for(Object subTaskHash:subTaskHashes.getDatas()){
			JSONObject result = new JSONObject();
			result.put("subTaskHash",subTaskHash);
			result.put("state",getHistoryTaskByTaskHash((String)subTaskHash).getState());
			resultArray.add(result);
		}
		return resultArray;
	}
	public HistoryTask getHistoryTaskByTaskHash(String taskHash) {
		String sql = "select * from history_task, user where history_task.create_person=user.user_id and  task_hash = ?";
		return (HistoryTask) Query.executeQueryObject(sql, new Object[]{taskHash}, new QueryCallBack() {

			@Override
			public Object doExecute(ResultSet rs) {
				HistoryTask task = new HistoryTask();
				try {
					while(rs.next()){
						task.setCreate_person(rs.getString("user_name"));
						task.setEnd_time(rs.getString("end_time"));
						task.setIntroduction(rs.getString("introduction"));
						task.setIs_nesting(rs.getBoolean("is_nesting"));
						task.setJson(rs.getString("json"));
						task.setModel_id(Integer.parseInt(rs.getString("model_id")));
						task.setName(rs.getString("name"));
						task.setNest_json(rs.getString("nest_json"));
						task.setStart_time(rs.getString("start_time"));
						task.setState(rs.getString("state"));
						task.setTask_hash(rs.getString("task_hash"));
						task.setTask_id(Integer.parseInt(rs.getString("task_id")));
						task.setAlgorithmOrders(rs.getString("algorithm_order"));
					}
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
				return task;
			}
		});

	}

	public static boolean updateJobAfterKilljob(String taskHash) {
		String sql = "update `history_job` set end_time = now(),isSuccess='DONE' where task_hash = ?";
		int result = Query.executeDML(sql, new Object[] { taskHash });
		if (result > 0)
			return true;
		else
			return false;
	}

	public static boolean updateTaskAfterKilljob(String taskHash) {

		String sql = "update `history_task` set end_time = now(),state = '空三任务终止'   where batch_task_hash=? or task_hash=?";
		int result = Query.executeDML(sql, new Object[] { taskHash, taskHash });
		if (result > 0)
			return true;
		else
			return false;
	}
	public static List<Object> getSubTaskByTaskHash(String taskHash){

		String sql = "select task_hash,commit_time,task_id,start_time,end_time,isSuccess  from history_task where task_hash=?";
		List<Object> history_jobs = new ArrayList<>();
		history_jobs = Query.executeQueryList(history_jobs, sql, new Object[]{taskHash}, new QueryCallBackList(){
			@Override
			public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
				try {
					while (rs.next()) {
						HistoryJob ht = new HistoryJob();
						ht.setTaskHash(rs.getString("task_hash"));
						ht.setJobId(rs.getInt("job_id"));
						ht.setStartTime(rs.getString("start_time"));;
						ht.setEndTime(rs.getString("end_time"));
						ht.setCommitTime(rs.getString("commit_time"));
						ht.setIsSuccess(rs.getString("isSuccess"));
						results.add(ht);

					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return results;
			}
		});
		return history_jobs;
	}
	public static List<Object> getHisTaskByTaskid(int taskid){

		String sql = "select task_hash,batch_task_hash,task_id,start_time,end_time,statement  from history_task where task_id=?";
		List<Object> history_tasks = new ArrayList<>();
		history_tasks = Query.executeQueryList(history_tasks, sql, new Object[]{taskid}, new QueryCallBackList(){
			@Override
			public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
				try {
					while (rs.next()) {
						HistoryTask ht = new HistoryTask();
						ht.setTask_hash(rs.getString("task_hash"));
						ht.setTask_id(rs.getInt("task_id"));
						ht.setStart_time(rs.getString("start_time"));;
						ht.setEnd_time(rs.getString("end_time"));
						ht.setStatement(rs.getString("statement"));
						ht.setBatch_task_hash(rs.getString("batch_task_hash"));
						results.add(ht);

					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return results;
			}
		});
		return history_tasks;

	}
	public boolean updateJsonPath(String json, String id) {
		String sql = "update `history_task` set json = ? where task_id = ?";
		int result = Query.executeDML(sql, new Object[] { json, id });
		if (result > 0)
			return true;
		else
			return false;
	}

	public List<Object> selectHistTask(){
		String sql = "select task_hash,task_id,json  from history_task where task_id=0  ";
		List<Object> history_tasks_days = new ArrayList<>();
		history_tasks_days = Query.executeQueryList(history_tasks_days, sql, null, new QueryCallBackList(){
			@Override
			public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
				try {
					while (rs.next()) {
						HistoryTask ht = new HistoryTask();
						ht.setTask_hash(rs.getString("task_hash"));
						ht.setTask_id(rs.getInt("task_id"));
						ht.setJson(rs.getString("json"));
						results.add(ht);

					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return results;
			}
		});
		return history_tasks_days;
	}

	public boolean addBatchTask(Task task, String taskHash, String orders,Date date) {
		String sql = "insert into `history_task` (task_hash,task_id,model_id,name,"
				+ "create_person,start_time,json,is_nesting,nest_json,introduction,algorithm_order) "
				+ "values (?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{taskHash,task.getTask_id(),task.getModel_id(),
				task.getName(),task.getUserId(),date,task.getJson(),task.isIs_nesting(),
				task.getNest_json(),task.getIntroduction(),orders};
		int result = Query.executeDML(sql, params);
		return result > 0;
	}

	public HttpResultList getSubTaskHashByBatchTaskHash(String taskHash) {

		String sql = "select task_hash from `history_task` where batch_task_hash = ?";
		return (HttpResultList) Query.executeQueryObject(sql, new Object[]{taskHash}, new QueryCallBack() {

			@Override
			public Object doExecute(ResultSet rs) {
				HttpResultList resultList = new HttpResultList();
				List<Object> result = new ArrayList<>();
				try {
					while(rs.next()){
						result.add(rs.getString("task_hash"));
					}
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
				resultList.setDatas(result);
				return resultList;
			}
		});

	}

	/**
	 * 查询某个批任务下的finished、stopped、pause、running状态的子任务数量
	 */
	public int getSubTaskStatement(String batchTaskHash, String statement) {
		String sql = "select count(1) from history_task where task_hash=? and statement=?";
		return PublicDao.oneNum(sql, new Object[]{batchTaskHash, statement});
	}
	/**
	 * 分页查询子任务信息
	 */
	public List<Object> getSubTaskHashStateByPage(String batchTaskHash, int onePageCount, int currentPage) {
		String sql = "select task_hash, statement from `history_task` where batch_task_hash = ? " +
				"order by task_hash asc limit " + (currentPage - 1) * onePageCount + " , " + onePageCount;
		List<Object> results = new ArrayList<>();
		return Query.executeQueryList(results, sql, new Object[]{batchTaskHash}, new QueryCallBackList() {
			@Override
			public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
				try {
					while(rs.next()){
						JSONObject oneResult = new JSONObject();
						oneResult.put("subTaskHash",rs.getString("task_hash"));
						oneResult.put("statement",stateTrans(rs.getString("statement")));
						results.add(oneResult);
					}
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
				return results;
			}
		});
	}

	/**
	 * 查询子任务数量
	 */
	public int getSubTaskNum(String batchTaskHash) {
		String sql = "select count(1) from history_task where batch_task_hash = ?";
		return PublicDao.oneNum(sql,new Object[]{batchTaskHash});
	}
	/**
	 * 查询共有多少页
	 */
	public int getPageNum(String batchTaskHash, int onePageCount) {
		int subTaskNum = this.getSubTaskNum(batchTaskHash);
		return (subTaskNum % onePageCount) == 0 ? (subTaskNum / onePageCount) : (subTaskNum / onePageCount + 1);
	}

	/**
	 * 查询批任务算法顺序
	 */
	public String getAlgorithmOrder(String batchTaskHash) {
		String sql = "select algorithm_order from history_task where task_hash = ?";
		return (String) Query.executeQueryObject(sql, new Object[]{batchTaskHash}, new QueryCallBack() {
			@Override
			public Object doExecute(ResultSet rs) {
				String results = null;
				try {
					while(rs.next()){
						results = rs.getString("algorithm_order");
					}
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
				return results;
			}
		});
	}
	/**
	 * 查询该批任务下任务完成数量
	 */
	public int getTaskFinishedNum(String batchTaskHash) {
		String sql = "select count(1) from history_task where batch_task_hash=? and not isnull(end_time)";
		return PublicDao.oneNum(sql, new Object[]{batchTaskHash});
	}
	/**
	 * 查询该批任务下算法完成数量
	 */
	public int getAlgorithmFinishedNum(String batchTaskHash, String algorithmHash) {
		String sql = "select count(1) from history_task as t,history_algorithm as a where t.batch_task_hash=? and " +
				"t.task_hash=a.task_hash and a.algorithm_hash=? and not isnull(a.end_time)";
		return PublicDao.oneNum(sql, new Object[]{batchTaskHash, algorithmHash});
	}
	public void recordSubTaskStatement(String statement){
	}

	/**
	 * 将历史任务表中的任务状态转化成前端识别的状态
	 */
	private String stateTrans(String state){
		String result;
		switch (state){
			case "finished":
				result = "DONE";
				break;
			case "stopped":
				result = "STOP";
				break;
			case "pause":
				result = "PAUSE";
				break;
			case "running":
				result = "RUN";
				break;
			default:
				result = "PEND";
				break;
		}
		return  result;
	}
}